'use strict';

const { Sequelize, Op } = require('sequelize');

const { findRouter } = require('../utils/tools.js')
const {db_database,db_host,db_uid,db_pwd,db_dialct}=require('../config');

let db = new Sequelize(db_database, db_uid, db_pwd, {
    host: db_host,
    dialect: db_dialct
});

let obj = {};

let files = findRouter('./model');

files.forEach(item => {// 'Role.js'
    let upperFileName = item.replace('.js', '');
    let lowerFileName = upperFileName.toLowerCase();
    let tmpObj = require('../model/' + item);
    obj[upperFileName] = db.define(lowerFileName, tmpObj, {
        tableName: 'app_' + lowerFileName,
        charset:'utf8'
    })
});

obj.Op = Op

obj.sync = async (force) => {
    if (force) {
        return db.sync({ force: true });
    }
    return db.sync();
}

module.exports = obj;